package org.fastsyncer.parser.enums;

import org.apache.commons.lang.StringUtils;
import org.fastsyncer.listener.ListenerException;
import org.fastsyncer.parser.convert.Handler;
import org.fastsyncer.parser.convert.handler.*;

/**
 * 支持的转换类型
 *
 * @author AE86
 * @version 2.0.0
 * @date 2019/9/19 23:56
 */
public enum ConvertEnum {

    /**
     * 默认转换
     */
    DEFAULT("default", new DefaultHandler()),
    /**
     * 默认时间戳
     */
    DEFAULT_TIMESTAMP("defaultTimestamp", new DefaultTimestampHandler()),
    /**
     * 替换,例如“张?三”替换?为空字符串 => 张三
     */
    REPLACE("replace", new ReplaceHandler()),
    /**
     * 追加在前面,例如“张三”追加123 => 123张三
     */
    PREPEND("prepend", new PrependHandler()),
    /**
     * 追加在后面,例如“张三”追加123 => 张三123
     */
    APPEND("append", new AppendHandler()),
    /**
     * AES加密
     */
    AES_ENCRYPT("aesEncrypt", new AesEncryptHandler()),
    /**
     * AES解密
     */
    AES_DECRYPT("aesDecrypt", new AesDecryptHandler()),
    /**
     * SHA1加密
     */
    SHA1("sha1", new Sha1Handler()),
    /**
     * UUID
     */
    UUID("uuid", new UUIDHandler()),
    /**
     * 去掉首字符
     */
    REM_STR_FIRST("remStrFirst", new RemStrFirstHandler()),
    /**
     * 去掉尾字符
     */
    REM_STR_LAST("remStrLast", new RemStrLastHandler()),
    /**
     * 从前面截取N个字符
     */
    SUB_STR_FIRST("subStrFirst", new SubStrFirstHandler()),
    /**
     * 从后面截取N个字符
     */
    SUB_STR_LAST("subStrLast", new SubStrLastHandler()),
    /**
     * 补位数字,例如“0000” => 0012
     */
    PICKUP_NUM("pickupNum", new PickUpNumHandler()),
    /**
     * 清空
     */
    CLEAR("clear", new ClearHandler());

    // 转换名称
    private String name;

    // 转换策略
    private Handler handler;

    ConvertEnum(String name, Handler handler) {
        this.name = name;
        this.handler = handler;
    }

    public static Handler getHandler(String name) throws ListenerException {
        for (ConvertEnum e : ConvertEnum.values()) {
            if (StringUtils.equals(name, e.getName())) {
                return e.getHandler();
            }
        }
        throw new ListenerException(String.format("Handler name \"%s\" does not exist.", name));
    }

    public String getName() {
        return name;
    }

    public Handler getHandler() {
        return handler;
    }

}
